package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2021/5/21
 * @description
 */
public class ETF {

    static String url = "http://yunhq.sse.com.cn:32041//v1/sh1/list/exchange/ebs?select=code%2Cname%2Copen%2Chigh%2Clow%2Clast%2Cprev_close%2Cchg_rate%2Cvolume%2Camount%2Ccpxxextendname%2Ctradephase&_=1621588753872";

    public static void main(String[] args) {
        getDns(url);
    }


    public static void getDns(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    JsonRootBean jsonRootBean = GsonUtil.parse(html, JsonRootBean.class);
                    List<EtfItem> list = new ArrayList<>();
                    for (List<String> item : jsonRootBean.getList()) {
                        EtfItem etfItem = new EtfItem();
                        etfItem.code = item.get(0);
                        etfItem.name = item.get(1);
                        etfItem.kpj = Double.parseDouble(item.get(2));
                        etfItem.zgj = Double.parseDouble(item.get(3));
                        etfItem.zdj =  Double.parseDouble(item.get(4));
                        etfItem.dqj =  Double.parseDouble(item.get(5));
                        etfItem.qrspj =  Double.parseDouble(item.get(6));
                        etfItem.zdf =  Double.parseDouble(item.get(7));
                        etfItem.cjl =  Double.parseDouble(item.get(8));
                        etfItem.cje =  Double.parseDouble(item.get(9));
                        etfItem.kzmc = item.get(10);
                        list.add(etfItem);
                    }
                    fenxi(list);
                }
            }
        });
    }


    public static class EtfItem {
        public String code; // 证券代码
        public String name;//证券简称
        public double kpj;//	开盘价
        public double zgj;//最高价
        public double zdj;//最低价
        public double dqj;//当前价
        public double qrspj; //前日收盘价
        public double zdf; //涨跌幅
        public double cjl; //成交量(手)
        public double cje; //成交额(万元)
        public String kzmc;//基金扩位简称
    }


    private static void fenxi(List<EtfItem> list1) {
        List<EtfItem> list = list1;
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                if (list.get(j).cje > list.get(j + 1).cje) {
                    EtfItem temp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, temp);
                }
            }
        }

        for (EtfItem item : list) {
            System.out.println( item.name);
            System.out.println("成交额(万元) " + item.cje);
            System.out.println("");
        }

    }


    public class JsonRootBean {

        private long date;
        private long time;
        private int total;
        private int begin;
        private int end;
        private List<List<String>> list;

        public void setDate(long date) {
            this.date = date;
        }

        public long getDate() {
            return date;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public long getTime() {
            return time;
        }

        public void setTotal(int total) {
            this.total = total;
        }

        public int getTotal() {
            return total;
        }

        public void setBegin(int begin) {
            this.begin = begin;
        }

        public int getBegin() {
            return begin;
        }

        public void setEnd(int end) {
            this.end = end;
        }

        public int getEnd() {
            return end;
        }

        public void setList(List<List<String>> list) {
            this.list = list;
        }

        public List<List<String>> getList() {
            return list;
        }

    }


}
